package com.lumlord.db.influx;


import lombok.extern.slf4j.Slf4j;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * influxdb 链接
 */
@Configuration
@Slf4j
public class InfluxDBConfiguration {

    @Autowired
    private InfluxDBProperties influxDBProperties;

    /**
     * 连接时序数据库 ，若不存在则创建
     *
     * @return
     */
    @Bean
    public InfluxDB influxDB() {
        // InfluxDB  influxDB = InfluxDBFactory.connect(influxDBProperties.getUrl(), influxDBProperties.getUserName(), influxDBProperties.getPassWord());

         InfluxDB  influxDB = InfluxDBFactory.connect(influxDBProperties.getUrl());
         influxDB.setLogLevel(InfluxDB.LogLevel.FULL);
         influxDB.setLogLevel(InfluxDB.LogLevel.NONE);
        return influxDB;
    }

    @Bean
    public InfluxDBConnection influxDBConnection(@Autowired InfluxDB influxDB){
        InfluxDBConnection influxDBConnection = new InfluxDBConnection();
        influxDBConnection.setDatabase(influxDBProperties.getDatabase());
        influxDBConnection.setInfluxDB(influxDB);
        influxDBConnection.setRetentionPolicy(influxDBProperties.getRetentionPolicy());


        return  influxDBConnection;
    }

}